/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : my_modify.js                            
*  Description  : 마이홈 정보관리
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-05-12  김효순    Created.  
*  2012-05-12  김효순    Complete.
*******************************************************************************/
jQuery(function($){
	// 아이디
	function clsId()
	{
		this._HTMLDom = null;
		
		this._init();
	}
	
	clsId.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content li:first");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			$layer.contents().filter(function(){
				if(this.nodeType == 3) return true;
				else return false;
			}).remove();
		}
		, setValue: function(val){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.append(val);
		}
	}
	
	// 닉네임
	function clsNick()
	{
		this._HTMLDom = null;
		this._ERRMSG = {
			"_NONICK": "닉네임을 입력해주세요."
			, "_NOCHECK": "닉네임 중복확인을 해주세요."
			, "_INVALID": "닉네임은 2~6 이하만 가능합니다."
		}
		this.errMsg = null;
		
		this._init();
	}
	
	clsNick.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content input:text:eq(0)");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			$layer.data("isChecked", false); // 중복확인 여부
		}
		, getValue: function(){
			var $layer = this._HTMLDom["$layer"];
			
			return $layer.val();
		}
		, setValue: function(val){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.val(val);
		}
		, setCheck: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.data("isChecked", true);
		}
		, getError: function(){
			if(this.errMsg == null) return null;
			else return this._ERRMSG[this.errMsg];
		}
		, isValid: function(){
			var $layer = this._HTMLDom["$layer"];
			
			this.errMsg = null;
			if(this.getValue().length == 0){
				this.errMsg = "_NONICK";
				return false;
			}else if(this.getValue().length < 2 || this.getValue().length > 6){
				this.errMsg = "_INVALID";
				return false;
			}else if(!$layer.data("isChecked")){
				this.errMsg = "_NOCHECK";
				return false;
			}
			
			return true;
		}
		, focus: function(){
			this._HTMLDom["$layer"].trigger("focus");
		}
		, $getObj: function(){
			return this._HTMLDom["$layer"];
		}
	}
	
	// 이메일
	function clsEmail()
	{
		this._HTMLDom = null;
		this._ERRMSG = {
			"_NOVALUE": "이메일을 입력해주세요."
			, "_INVALID": "정상적인 이메일주소가 아닙니다."
		}
		this.$errObj = null;
		this.errMsg = null;
		
		this._init();
	}
	
	clsEmail.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content").find("input:text:eq(1), input:text:eq(2)");
			this._HTMLDom = {
				"$layer": $layer
			}
		}
		, getError: function(){
			if(!this.errMsg) return null;
			else return this._ERRMSG[this.errMsg];
		}
		, isValid: function(){
			var $layer = this._HTMLDom["$layer"];
			var val1 = $layer.eq(0).val();
			var val2 = $layer.eq(1).val();
			var reg1 = /^[\w\-\.]+$/ig;
			var reg2 = /^(?:(?:[\w\-]+\.)+[a-zA-Z]{2,})$/ig;
			
			this.errMsg = null;
			this.$errObj = null;
			if(val1.length == 0){
				this.errMsg = "_NOVALUE";
				this.$errObj = $layer.eq(0);
				return false;
			}else if(val2.length == 0){
				this.errMsg = "_NOVALUE";
				this.$errObj = $layer.eq(1);
				return false;
			}else if(!reg1.test(val1)){
				this.errMsg = "_INVALID";
				this.$errObj = $layer.eq(0);
				return false;
			}else if(!reg2.test(val2)){
				this.errMsg = "_INVALID";
				this.$errObj = $layer.eq(1);
				return false;
			}
			
			return true;
		}
		, setValue: function(n, val){
			var $layer = this._HTMLDom["$layer"];
			$layer.eq(n).val(val);
		}
		, getValue: function(){
			var $layer = this._HTMLDom["$layer"];
			var val1 = $layer.eq(0).val();
			var val2 = $layer.eq(1).val();
			
			return val1 + "@" + val2;
		}
		, focus: function(){
			if(this.$errObj) this.$errObj.trigger("focus");
			else this._HTMLDom["$layer"].eq(0).trigger("focus");
		}
	}
	
	// 이벤트 및 정보메일 수신
	function clsReceive()
	{
		this._HTMLDom = null;
		
		this._init();
	}
	
	clsReceive.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content input:checkbox");
			this._HTMLDom = {
				"$layer": $layer
			}
		}
		, getValue: function(){
			if(this._HTMLDom["$layer"].attr("checked")) return "Y";
			else return "N";
		}
		, setCheck: function(){
			this._HTMLDom["$layer"].attr("checked", "checked");
		}
	}
	
	// 이메일 선택박스
	function clsEmailSelect()
	{
		this._HTMLDom = null;
		
		this._init();
	}
	
	clsEmailSelect.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content select");
			this._HTMLDom = {
				"$layer": $layer
			}
		}
		, $getObj: function(){
			return this._HTMLDom["$layer"];
		}
		, getValue: function(){
			return this._HTMLDom["$layer"].find("option:selected").val();
		}
	}
	
	// 서버통신
	function clsComm()
	{
		// 회원정보
		this.memberInfo = null;
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
			this.memberInfo = {
				"아이디": null
				, "닉네임": null
				, "이메일1": null
				, "이메일2": null
				, "성별": null
				, "유저레벨": null
				, "정보수신여부": null
			}
		}
		, getMemberInfo: function(){
			var thisClass = this;
			
			document.objGlobal.ajax({
				"sid":"US001",
				"servicename":"getMyUserInfo",
				"listcount":10,
				"currpageno":1
			}, {
				
			}, function(ret){
				var obj = ret["msp"]["contents"]["items"];
				var aEmail = obj["email"].split("@");
				
				thisClass.memberInfo = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": document.objGlobal.getResultMsg(ret)
					, "아이디": obj["userId"]
					, "닉네임": obj["nickname"]
					, "이메일1":aEmail[0]
					, "이메일2": aEmail[1]
					, "성별": obj["sex"]
					, "정보수신여부": obj["emailreceiveYn"]
				}
				document.objControl.finishMemberInfo();
			});
			
			//ajax
			/*setTimeout(function(){
				thisClass.memberInfo = {
					"아이디": "sky72"
					, "닉네임": "스카이72"
					, "이메일1": "sky72"
					, "이메일2": "sky72.com"
					, "성별": "M"
					, "유저레벨": "A"
					, "정보수신여부": "Y"
				}
				
				document.objControl.finishMemberInfo();
			}, 1000);*/
		}
		, getMemberData: function(){
			return this.memberInfo;
		}
		, memberSubmit: function(arg){
			var thisClass = this;
			
			/*arg = {
				"닉네임": null
				, "이메일": null
				, "정보수신여부": null // YN
			}*/
			
			//ajax
			document.objGlobal.ajax({
				"sid":"US001",
				"servicename":"update",
				"listcount":10,
				"currpageno":1
			}, {
				"email": arg["이메일"]
				, "nickname": arg["닉네임"]
				, "userType": 1
				, "sex": thisClass.memberInfo["성별"]
				, "emailreceiveYn": arg["정보수신여부"]
				, "userLevel": thisClass.memberInfo["유저레벨"]
			}, function(ret){
				document.objControl.finishMemberSubmit({
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": document.objGlobal.getResultMsg(ret)
				});
			});
			
			/*setTimeout(function(){
				var rnd = document.objGlobal.random(0, 1);
				var ret = {
					"결과코드": rnd
					, "메시지": null
				}
				
				document.objControl.finishMemberSubmit(ret);
			}, 1000);*/
		}
	}
	
	// 컨트롤
	function clsControl()
	{
		this.objComm = null;
		this.objId = null;
		this.objNick = null;
		this.objEmail = null;
		this.objReceive = null;
		this.objEmailSelect = null;

		this._init();
	}
	
	clsControl.prototype = {
		_init: function(){
			if(document.objGlobal.getCookie("check_auth") != "Y") document.location.href = "my_info_pw.html";
			document.objGlobal.delCookie("check_auth", "/");
			
			this.objComm = new clsComm();
			this.objId = new clsId();
			this.objNick = new clsNick();
			this.objEmail = new clsEmail();
			this.objReceive = new clsReceive();
			this.objEmailSelect = new clsEmailSelect();
			
			$("div#container input:text").keydown(function(event){
				if(event.keyCode == 13) return false;
			});
			
			this._bind();
			
			// 회원정보 받아오기
			document.objGlobal.objModalLoading.open("receiving...");
			this.objComm.getMemberInfo();
		}
		, _bind: function(){
			var thisClass = this;
			
			// 중복체크 버튼
			$("div#container input:image[alt='중복확인']").click(function(event){
				event.preventDefault();
				
				thisClass.openNickUniq();
			});
			
			// 닉네임 엔터시
			this.objNick.$getObj().keydown(function(event){
				if(event.keyCode == 13) thisClass.openNickUniq();
			});
			
			// 이메일 선택
			this.objEmailSelect.$getObj().change(function(){
				thisClass.objEmail.setValue(1, thisClass.objEmailSelect.getValue());
			});
			
			// 전송
			$("div#container img[alt='확인']").parent("a").click(function(){
				if(!thisClass.objNick.isValid()){
					alert(thisClass.objNick.getError());
					thisClass.objNick.focus();
					return;
				}else if(!thisClass.objEmail.isValid()){
					alert(thisClass.objEmail.getError());
					thisClass.objEmail.focus();
					return;
				}
				
				document.objGlobal.objModalLoading.open("sending...");
				thisClass.objComm.memberSubmit({
					"닉네임": thisClass.objNick.getValue()
					, "이메일": thisClass.objEmail.getValue()
					, "정보수신여부": thisClass.objReceive.getValue()
				});
			});
		}
		// 회원정보 완료
		, finishMemberInfo: function(){
			var thisData = this.objComm.getMemberData();
			
			document.objGlobal.objModalLoading.close();
			
			if(thisData["메시지"]) alert(thisData["메시지"]);
			if(thisData["결과코드"] != "00000"){
				document.location.href = "my_home.html";
				return;
			}
			
			this.objId.setValue(thisData["아이디"]);
			this.objNick.setValue(thisData["닉네임"]);
			this.objEmail.setValue(0, thisData["이메일1"]);
			this.objEmail.setValue(1, thisData["이메일2"]);
			if(thisData["정보수신여부"] == "Y") this.objReceive.setCheck();
		}
		// 닉네임 중복확인팝업
		, openNickUniq: function(){
			var winNickCheck = window.open("../popup/id_check.html?q_type=nick", "winNickCheck", "width=500, height=500, scrollbars=no");
			if(winNickCheck) winNickCheck.focus();
		}
		// 중복체크로부터 사용하기
		, returnCheck: function(w, q_type, val){
			if(w) w.close();
			
			var obj = null;
			
			switch(q_type){
				case "id":
					obj = this.objId;
					break;
				case "nick":
					obj = this.objNick;
					break;
			}
			
			obj.setValue(val);
			obj.setCheck();
		}
		// 중복체크 오픈시 초기값 전송
		, getValue: function(q_type){
			switch(q_type){
				case "id":
					return this.objId.getValue()
					break;
				case "nick":
					return this.objNick.getValue();
					break;
			}
		}
		// 전송완료
		, finishMemberSubmit: function(ret){
			document.objGlobal.objModalLoading.close();
			
			if(ret["메시지"]) alert(ret["메시지"]);
			//if(ret["결과코드"] != "00000") return;
		}
	}
	
	document.objControl = new clsControl();
});
